<?php

namespace app\admin\controller;

use app\common\business\Goods as GoodsBis;

class Goods extends AdminBase {

    /**
     * 商品列表
     * @return \think\response\View
     */
    public function index() {
        $data = [];
        $title = input('param.title', '', 'trim');
        $time = input('param.time', '', 'trim');
        if (!empty($title)) {
            $data['title'] = $title;
        }
        if (!empty($time)) {
            $data['create_time'] = explode(' - ', $time);
        }

        $goods = (new GoodsBis())->getLists($data, 5);
        return view('', [
            'goods' => $goods,
            'title' => $title,
            'time' => $time
        ]);
    }

    public function add() {
        return view();
    }

    /**
     * 新增逻辑
     * @return \think\response\Json
     */
    public function save() {
        if (!$this->request->isPost()) {
            return show(config('status.error'), '参数不合法');
        }

        // TODO 验证参数, 判断数据类型
        $data = input('param.');
        //halt($data);
        // 表单令牌验证
        $check = $this->request->checkToken('__token__');
        if ($check === false) {
            //return show(config('status.error'), '非法请求');
        }

        // 数据处理 = > 基于 我们得验证成功之后
        $data['category_path_id'] = $data['category_id'];
        $result = explode(',', $data['category_path_id']);
        $data['category_id'] = end($result);
        if (!$data['category_id']) {
            return show(config('status.error'), '商品分类有误');
        }

        $res = (new GoodsBis())->insertData($data);
        if (!$res) {
            return show(config('status.error'), '商品新增失败');
        }

        return show(config('status.success'), '商品新增成功');
    }

}